package com.warrior.service;

import com.warrior.entity.HomeIntroductionEntity;
import com.warrior.entity.IntroductionContentEntity;
import com.warrior.respository.IHomeIntroductionRepository;
import com.warrior.respository.IIntroductionContentRepository;
import com.warrior.util.DateUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by Shawn on 2017/3/20.
 */
@Service
public class HomeIntroductionService {

    private static final Logger logger = LogManager.getLogger(HomeIntroductionService.class.getName());

    @Autowired
    private IHomeIntroductionRepository homeIntroductionRepository;

    @Autowired
    private IIntroductionContentRepository introductionContentRepository;


    /**
     * 保存首页信息
     *
     * @param homeIntroductionEntity 首页信息
     */
    @Transactional
    public void saveHomeIntroduction(HomeIntroductionEntity homeIntroductionEntity) {
        this.homeIntroductionRepository.save(homeIntroductionEntity);
    }

    /**
     * 保存首页内部信息
     *
     * @param introductionContentEntity 内部信息
     */
    @Transactional
    public void saveIntroductionContent(IntroductionContentEntity introductionContentEntity) {
        if (logger.isDebugEnabled()) {
            logger.debug("saveIntroductionContent(IntroductionContentEntity introductionContentEntity={}) - start", introductionContentEntity); //$NON-NLS-1$
        }

        this.introductionContentRepository.save(introductionContentEntity);

        if (logger.isDebugEnabled()) {
            logger.debug("saveIntroductionContent(IntroductionContentEntity) - end"); //$NON-NLS-1$
        }
    }

    /**
     * 根据主页id查找主页信息
     *
     * @param homeId 主页id
     * @return HomeIntroductionEntity
     */
    @Transactional
    public HomeIntroductionEntity findHomeIntroductionById(String homeId) {
        if (logger.isDebugEnabled()) {
            logger.debug("findHomeIntroductionById(String homeId={}) - start", homeId); //$NON-NLS-1$
        }

        HomeIntroductionEntity homeIntroductionEntity = this.homeIntroductionRepository.findHomeIntroductionById(homeId);

        if (logger.isDebugEnabled()) {
            logger.debug("findHomeIntroductionById(String) - end"); //$NON-NLS-1$
        }
        return homeIntroductionEntity;
    }

    /**
     * 根据内容id返回内容信息
     *
     * @param contentId 内容id
     * @return IntroductionContentEntity
     */
    @Transactional
    public IntroductionContentEntity findContentById(String contentId) {
        if (logger.isDebugEnabled()) {
            logger.debug("findContentById(String contentId={}) - start", contentId); //$NON-NLS-1$
        }

        IntroductionContentEntity introductionById = this.introductionContentRepository.findIntroductionById(contentId);

        if (logger.isDebugEnabled()) {
            logger.debug("findContentById(String) - end"); //$NON-NLS-1$
        }
        return introductionById;
    }


    /**
     * 根据主页id查找所属楼层信息
     *
     * @param homeContentId 楼层id
     * @return introductionContentByHomeIntroductionId
     */
    @Transactional
    public List<IntroductionContentEntity> findContentByHomeId(String homeContentId) {
        if (logger.isDebugEnabled()) {
            logger.debug("findContentByHomeId(String homeContentId={}) - start", homeContentId); //$NON-NLS-1$
        }

        List<IntroductionContentEntity> introductionContentByHomeIntroductionId =
                this.introductionContentRepository.findIntroductionContentByIntroductionIdOrderByIntroductionSequenceAsc(homeContentId);

        if (logger.isDebugEnabled()) {
            logger.debug("findContentByHomeId(String) - end"); //$NON-NLS-1$
        }
        return introductionContentByHomeIntroductionId;
    }


    /**
     * 更新一条记录
     *
     * @param content   需要修改的内容
     * @param type      0表示文本,1表示图片路径,2表示视频路径
     * @param sequence  序列
     * @param contentId 模块id
     */
    @Transactional
    public void updateContent(String content, int type, int sequence, String contentId) {
        if (logger.isDebugEnabled()) {
            logger.debug("updateContent(String content={}, int type={}, int sequence={}, String contentId={}) - start", content, type, sequence, contentId); //$NON-NLS-1$
        }

        IntroductionContentEntity contentEntity = this.findContentById(contentId);
        switch (type) {
            case 1:
                contentEntity.setIntroductionText(content);
                break;
            case 2:
                contentEntity.setIntroductionPicPath(content);
                break;
            case 3:
                contentEntity.setIntroductionVideoPath(content);
                break;
            default:
                break;
        }
        contentEntity.setIntroductionSequence(sequence);
        DateUtil dateUtil = new DateUtil();
        contentEntity.setUpdateAt(dateUtil.Date2Timestamp(new Date()));
        this.saveIntroductionContent(contentEntity);

        if (logger.isDebugEnabled()) {
            logger.debug("updateContent(String, int, int, String) - end"); //$NON-NLS-1$
        }
    }

    @Transactional
    public Page<HomeIntroductionEntity> listAllHomeIntroduction(int size, int page) {
        if (logger.isDebugEnabled()) {
            logger.debug("listAllHomeIntroduction(int size={}, int page={}) - start", size, page); //$NON-NLS-1$
        }

        Sort sort = new Sort(Sort.Direction.ASC, "sequence");
        //设置分页参数
        Pageable pageable = new PageRequest(page, (size <= 0) ? 3 : size, sort);
        Page<HomeIntroductionEntity> allOrderBySequence = this.homeIntroductionRepository.findAll(pageable);

        if (logger.isDebugEnabled()) {
            logger.debug("listAllHomeIntroduction(int, int) - end"); //$NON-NLS-1$
        }
        return allOrderBySequence;
    }

    @Transactional
    public void deleteHomeIntroductionContent(String contentId){
        this.introductionContentRepository.delete(contentId);
    }


    @Transactional
    public void deleteHomeIntroduction(String homeIntroductionId){
        //先找到所有内容,先删除子内容
        List<IntroductionContentEntity> contentEntities = this.findContentByHomeId(homeIntroductionId);
        for(IntroductionContentEntity ic : contentEntities){
            this.deleteHomeIntroductionContent(ic.getId());
        }
        this.homeIntroductionRepository.delete(homeIntroductionId);
    }


}
